package com.opal.qualityControl.service.impl;

import com.opal.system.entity.table.OpalUser;
import com.opal.qualityControl.entity.CheckoutResult;
import com.opal.qualityControl.entity.RawMaterialForm;
import com.opal.qualityControl.DTO.RawMaterialResultDTO;
import com.opal.qualityControl.mapper.oracle.RawMaterialCheckMapper;
import com.opal.qualityControl.service.RawMaterialReportService;
import com.opal.utils.GrfTableVOUtils;
import com.opal.vo.GrfTableVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class RawMaterialReportServiceImpl implements RawMaterialReportService {

    @Autowired
    private RawMaterialCheckMapper rawMaterialCheckMapper;


    @Override
    public GrfTableVO<RawMaterialResultDTO> getRawMaterialResult(String batchCode ,List<CheckoutResult> checkoutResult) {
        RawMaterialForm rawMaterialForm = rawMaterialCheckMapper.findByBatchCode(batchCode);
        List<RawMaterialResultDTO> rawMaterialResultDTO = new ArrayList<>();
        Subject subject = SecurityUtils.getSubject();
        OpalUser user = (OpalUser) subject.getPrincipal();
        if (user==null){
            return GrfTableVOUtils.error(404,"登录已过期，请重新登录！");
        }
        if (checkoutResult != null) {
            for (CheckoutResult cc : checkoutResult) {
                RawMaterialResultDTO rr = new RawMaterialResultDTO();
                BeanUtils.copyProperties(cc, rr);
                if (rawMaterialForm != null) {
                    BeanUtils.copyProperties(rawMaterialForm, rr);
                }
                rawMaterialResultDTO.add(rr);
            }
        }

        return GrfTableVOUtils.success(rawMaterialResultDTO,200,"查询数据成功!");
    }

    @Override
    public List<CheckoutResult> getCheckoutResult(String batchCode) {
        List<CheckoutResult> checkoutResults = rawMaterialCheckMapper.getCheckoutResult(batchCode);
        return checkoutResults;
    }


}
